<template>
  <div class="box">
    <van-button class="btn" @click="isNull('conStart', conStart)" size="small" type="primary">启动</van-button>
    <van-button class="btn" @click="isNull('conStop', conStop)" size="small" type="warning">停止</van-button>
    <van-button class="btn" @click="isNull('conPause', conPause)" size="small" type="warning">暂停</van-button>
    <van-button class="btn" @click="isNull('conUnPause', conUnPause)" size="small" type="info">恢复</van-button>
    <van-button class="btn" @click="isNull('conDel', conDel)" size="small" type="danger">删除</van-button>
  </div>
</template>

<script>
import Vue from 'vue'
import { Button } from 'vant'

Vue.use(Button)
export default {
  props: ['checkItem'],
  methods: {
    // 容器检测
    isNull (name, fun) {
      // 检测是否有选中的容器
      if (this.checkItem.length === 0) {
        return this.$Toast('请选择容器!')
      }
      // 启动容器的判断
      if (name === 'conStart') {
        // 判断选中是容器是否是运行中的，或是暂停的容器
        const isrun = this.checkItem.filter(item => {
          return item.State === 'running' || item.State === 'paused'
        })
        if (isrun.length > 0) {
          return this.$Toast('选择容器中有已运行或暂停的容器！')
        }
      }
      // 停止容器 和 暂停容器 的判断
      if (name === 'conStop' || name === 'conPause') {
        // 判断选中容器是否已经停止，或暂停状态
        const isrun = this.checkItem.filter(item => {
          return item.State === 'exited' || item.State === 'paused'
        })
        if (isrun.length > 0) {
          return this.$Toast('选择容器中有已停止或暂停容器!')
        }
      }
      // 恢复容器的判断
      if (name === 'conUnPause') {
        const isrun = this.checkItem.filter(item => {
          return item.State !== 'paused'
        })
        if (isrun.length > 0) {
          return this.$Toast('选择容器中有未暂停或已停止容器!')
        }
      }
      // 删除容器的判断
      if (name === 'conDel') {
        const isrun = this.checkItem.filter(item => {
          return item.State === 'running' || item.State === 'paused'
        })
        if (isrun.length > 0) {
          this.isDel = confirm('选择有已运行或暂停容器，是否强制删除？')
          if (!this.isDel) {
            return
          }
        }
      }
      fun()
    },
    // 请求数据模板
    fetchData (item) {
      const parse = {
        data: {
          docker: item.docker
        }
      }
      this.$post(parse, res => {
        var data = JSON.parse(res)
        if (data.message) {
          return this.$Notify({ type: 'danger', message: data.message })
        }
        if (data >= 200 && data < 300) {
          this.$Notify({ type: 'success', message: item.operation + '容器成功！' })
          this.$emit('success')
        } else {
          this.$Notify({ type: 'danger', message: item.operation + '容器失败！' })
        }
      })
    },
    // 启动容器
    conStart () {
      // 循环调用请求函数
      for (var i = 0; i < this.checkItem.length; i++) {
        const item = {
          docker: `curl -w %{http_code} --unix-socket /var/run/docker.sock -s -X POST "http:/v1.40/containers/${this.checkItem[i].Id}/start"`,
          operation: '启动'
        }
        this.fetchData(item)
      }
    },
    // 停止容器
    conStop () {
      // 循环调用请求函数
      for (var i = 0; i < this.checkItem.length; i++) {
        const item = {
          docker: `curl -w %{http_code} --unix-socket /var/run/docker.sock -s -X POST "http:/v1.40/containers/${this.checkItem[i].Id}/stop"`,
          operation: '停止'
        }
        this.fetchData(item)
      }
    },
    // 暂停容器
    conPause () {
      // 循环调用请求函数
      for (var i = 0; i < this.checkItem.length; i++) {
        const item = {
          docker: `curl -w %{http_code} --unix-socket /var/run/docker.sock -s -X POST "http:/v1.40/containers/${this.checkItem[i].Id}/pause"`,
          operation: '暂停'
        }
        this.fetchData(item)
      }
    },
    // 恢复容器
    conUnPause () {
      // 循环调用请求函数
      for (var i = 0; i < this.checkItem.length; i++) {
        const item = {
          docker: `curl -w %{http_code} --unix-socket /var/run/docker.sock -s -X POST "http:/v1.40/containers/${this.checkItem[i].Id}/unpause"`,
          operation: '恢复'
        }
        this.fetchData(item)
      }
    },
    // 删除容器
    conDel () {
      // 循环调用请求函数
      for (var i = 0; i < this.checkItem.length; i++) {
        const item = {
          docker: `curl -w %{http_code} --unix-socket /var/run/docker.sock -s -X DELETE "http:/v1.40/containers/${this.checkItem[i].Id}?force=${this.isDel}"`,
          operation: '删除'
        }
        this.fetchData(item)
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.box{
  display: flex;
  justify-content: space-around;
  align-items: center;
  height: vw(80);
  margin-bottom: vw(10);
  .btn{
    border-radius: vw(15);
  }
}
</style>
